package com.jiuli.liping.service.impl;

import com.jiuli.liping.dao.CommodityDao;
import com.jiuli.liping.dao.FavoritesDao;
import com.jiuli.liping.dao.ProductDao;
import com.jiuli.liping.vo.Classification;
import com.jiuli.liping.vo.Commodity;
import com.jiuli.liping.vo.Favorites;
import com.jiuli.liping.vo.Photograph;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jiuli.liping.service.FavoritesService;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * Author zyl
 * Date  2020-06-08
 */
@Service
public class FavoritesServiceImpl implements FavoritesService {
    @Autowired
    private FavoritesDao favoritesDao;
    @Autowired
    private CommodityDao commodityDao;
    @Autowired
    private ProductDao productDao;

    
    @Override
    public Favorites get(String id){
        return favoritesDao.get(id);
    }
    
    @Override
    public List<Favorites> findList(Favorites favorites) {
        return favoritesDao.findList(favorites);
    }
    
    @Override
    public List<Favorites> findAllList() {
        return favoritesDao.findAllList();
    }
    
    @Override
    public int insert(Favorites favorites) {
        int a = favoritesDao.jyFavorites(favorites);
        if(a>0){
            return 0;
        }

        Timestamp curDate = new Timestamp(System.currentTimeMillis());
        favorites.setCreateTime(curDate);
        return favoritesDao.insert(favorites);
    }
    
    @Override
    public int insertBatch(List<Favorites> favoritess){
        return favoritesDao.insertBatch(favoritess);
    }
    
    @Override
    public int update(Favorites favorites) {
        return favoritesDao.update(favorites);
    }
    
    @Override
    public int delete(Favorites favorites) {
        return favoritesDao.delete(favorites);
    }

    @Override
    public List<Classification> getFavorites(String userNum) {
        List<Classification> list = favoritesDao.getFavorites(userNum);
        for(Classification classification: list) {
            List<Commodity> list2 = new ArrayList<>();
            for (Commodity commodity1: classification.getCommodityList()) {
                String min = commodityDao.findMinPrice(commodity1.getCommodityFatherNumber());
                if (min == null) {
                    commodity1.setPrice("0.00");
                } else {
                    commodity1.setPrice(min);
                }

                Commodity coo = new Commodity();
                coo.setSalesPrice(min);
                coo.setCommodityFatherNumber(commodity1.getCommodityFatherNumber());
                List<Photograph> li = commodityDao.findPictureUrl(coo);
                if (li != null) {
                    commodity1.setPicture1(li.get(0).getPhotographUrl());
                }
                int a = productDao.getCountCommodity(commodity1.getCommodityFatherNumber());
                if(a > 0){
                    list2.add(commodity1);
                }
            }
            classification.setCommodityList(list2);
        }
        return list;
    }

}
